package com.dearcocoa.controller;


import com.dearcocoa.common.BaseResult;
import com.dearcocoa.common.Status;
import com.dearcocoa.entity.User;
import com.dearcocoa.mapper.UserMapper;
import com.dearcocoa.service.IUserService;
import com.dearcocoa.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dearcocoa
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService iUserService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 用户信息
     * @param request
     * @return
     */
    @PostMapping("/info")
    public BaseResult userInfo(HttpServletRequest request) {
        String jwt = jwtUtil.getJwtFromRequest(request);
        User userInfo = iUserService.getUserInfo(jwt);
        return BaseResult.ofStatus(Status.SUCCESS, userInfo);
    }

    /**
     * 用户信息
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    public BaseResult getById(@PathVariable("id") String id) {
        User userInfo = iUserService.getById(id);
        return BaseResult.ofStatus(Status.SUCCESS, userInfo);
    }
}
